package com.taoyi.mhouse.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.taoyi.mhouse.R;

/**
 * Created by tempo on 2015/11/20.
 * this app's bottom navigation
 */
public class BottomNavigation extends FrameLayout implements ViewPager.OnPageChangeListener {
    private ViewGroup container;
    private int[][] imgs = {
            {R.drawable.tab_home_img_selector},
            {R.drawable.tab_finding_img_selector, R.drawable.tab_order_img_selector, R.drawable.tab_order_img_selector, R.drawable.tab_order_img_selector},
            {R.drawable.tab_message_img_selector, R.drawable.tab_car_img_selector, R.drawable.tab_car_img_selector, R.drawable.tab_car_img_selector,},
            {R.drawable.tab_my_img_selector}};
    private String[][] titles = {
            {"首页"},
            {"发现", "嗮单", "+++", "++++"},
            {"消息", "购物车", "+++", "++++"},
            {"我的"}
    };
    private OnPageChangedListener mOnPageChangedListener;
    private int currPage=-1;
    public BottomNavigation(Context context) {
        this(context, null);
    }

    public BottomNavigation(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BottomNavigation(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        container = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.bottom_nav, this, false);
        this.addView(container);
        for (int i = 0; i < container.getChildCount(); i++) {
            initChild(i, context);
        }
    }

    private void initChild(final int i, Context context) {
        final FrameLayout child = (FrameLayout) container.getChildAt(i);
        int childCount = 0;
        if (i == 0) {
            childCount = 1;
        } else if (i == 1) {
            childCount = 4;
        } else if (i == 2) {
            childCount = 4;
        } else if (i == 3) {
            childCount = 1;
        }

        for (int m = 0; m < childCount; m++) {
            View v = LayoutInflater.from(context).inflate(R.layout.bottom_tab, child, false);
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            v.setLayoutParams(params);
            child.addView(v);
            TextView tvBottom = (TextView) v.findViewById(R.id.tv_bottom);
            ImageView ivBottom = (ImageView) v.findViewById(R.id.iv_bottom);

            tvBottom.setEnabled(false);
            ivBottom.setEnabled(false);

            tvBottom.setText(titles[i][m]);
            ivBottom.setImageResource(imgs[i][m]);
        }
        child.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemChecked(i);
                Log.i("tag", "clicked:" + i);
            }
        });
    }

    private void onItemChecked(int item) {
        if(currPage==item)
            return;
        currPage=item;
        if(mOnPageChangedListener!=null){
            mOnPageChangedListener.onPageChanged(item);
        }
        for (int i = 0; i < container.getChildCount(); i++) {
            ViewGroup iChild = (ViewGroup) container.getChildAt(i);
            for (int j = 0; j < iChild.getChildCount(); j++) {
                View iiChild = iChild.getChildAt(j);
                iiChild.findViewById(R.id.tv_bottom).setEnabled(i == item ? true : false);
                iiChild.findViewById(R.id.iv_bottom).setEnabled(i == item ? true : false);
            }
        }
    }
    public void setOnPageChangeListener(OnPageChangedListener mOnPageChangedListener){
       this.mOnPageChangedListener=mOnPageChangedListener;
    }
    public interface OnPageChangedListener{
        public void onPageChanged(int page);
    }
    public void setInitPage(int i){
        if(mOnPageChangedListener==null){
            throw new NullPointerException("please setOnPageChangedListener before setInitPage!");
        }
        onPageScrolled(i,0,0);
        onItemChecked(i);
    }
    public void setViewPager(ViewPager viewPager) {
        viewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        for (int i = 0; i < container.getChildCount(); i++) {
            if (i == 1 || i == 2) {
                FrameLayout tab = (FrameLayout) container.getChildAt(i);
                float currPagePos = position + positionOffset;
                for (int j = 0; j < tab.getChildCount(); j++) {
                    View child = tab.getChildAt(j);
                    float tran = Math.abs(j - currPagePos) * 2;
                    float alpha = 0;
                    if (tran > 1)
                        alpha = 0;
                    else
                        alpha = 1 - tran;
                    child.setAlpha(alpha);

                }
            }

        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
